﻿// ===============================================================================
// XCI.WinForm.Platform 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;
using System.ComponentModel;
using System.Drawing;
using XCI.Core;
using XCI.Helper;

namespace XCI.Windows
{
    /// <summary>
    /// 数据字典下拉多选控件
    /// </summary>
    [System.ComponentModel.DesignerCategory("Code")]
    public class DicCheckedComboBoxEdit : CheckedComboBoxEdit
    {
        static DicCheckedComboBoxEdit()
        {
            RepositoryItemDicCheckedComboBoxEdit.RegisterDicCheckedComboBoxEdit();
        }

        public DicCheckedComboBoxEdit()
        {

        }

        /// <summary>
        /// 指示要为此控件中的项显示的属性
        /// </summary>
        [Category("XCI"), Description("指示要为此控件中的项显示的属性")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual string DisplayMember => "Name";

        /// <summary>
        /// 指示用作控件中项的实际值的属性
        /// </summary>
        [Category("XCI"), Description("指示用作控件中项的实际值的属性")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual string ValueMember => "Value";

        /// <summary>
        /// 字典编码
        /// </summary>
        [Category("XCI")]
        [Description("字典编码")]
        [DefaultValue(null)]
        public string DicCode { get; set; }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (WinFormHelper.IsDesignMode()) return;
            if (string.IsNullOrEmpty(DicCode)) return;

            base.Properties.ValueMember = "Value";
            base.Properties.DisplayMember = "Name";

            LoadDic(DicCode);
        }

        public void LoadDic(string code)
        {
            var dic = App.Handler.GetDic(code);
            base.Properties.DataSource = dic.Items;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemDicCheckedComboBoxEdit Properties => base.Properties as RepositoryItemDicCheckedComboBoxEdit;

        public override string EditorTypeName => RepositoryItemDicCheckedComboBoxEdit.CustomEditName;
    }

    [UserRepositoryItem("RegisterDicCheckedComboBoxEdit")]
    public class RepositoryItemDicCheckedComboBoxEdit : RepositoryItemCheckedComboBoxEditx
    {
        static RepositoryItemDicCheckedComboBoxEdit()
        {
            RegisterDicCheckedComboBoxEdit();
        }

        public new const string CustomEditName = "DicCheckedComboBoxEdit";

        public RepositoryItemDicCheckedComboBoxEdit()
        {
        }

        public override string EditorTypeName => CustomEditName;

        public static void RegisterDicCheckedComboBoxEdit()
        {
            Image img = null;
            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName, typeof(DicCheckedComboBoxEdit), typeof(RepositoryItemDicCheckedComboBoxEdit), typeof(DicCheckedComboBoxEditViewInfo), new DicCheckedComboBoxEditPainter(), true, img));
        }

        public override void Assign(RepositoryItem item)
        {
            BeginUpdate();
            try
            {
                base.Assign(item);
                if (!(item is RepositoryItemDicCheckedComboBoxEdit source)) return;
                //
            }
            finally
            {
                EndUpdate();
            }
        }
    }

    public class DicCheckedComboBoxEditViewInfo : CheckedComboBoxEditxViewInfo
    {
        public DicCheckedComboBoxEditViewInfo(RepositoryItem item) : base(item)
        {
        }
    }

    public class DicCheckedComboBoxEditPainter : CheckedComboBoxEditxPainter
    {
        public DicCheckedComboBoxEditPainter()
        {
        }
    }
}